﻿using System.Web;
using CRMBiz;
using CRMEntity;

namespace CRMWeb.Utilities.Context
{
    public class ContextData
    {
        private CmsMember mCurrentUser;

        public CmsMember CurrentUser
        {
            get
            {
                if (mCurrentUser == null)
                {
                    mCurrentUser = GetCurrentUser(true);
                }
                return mCurrentUser;
            }
            set
            {
                mCurrentUser = value;
                SetCurrentUser(value);
            }
        }

        public CmsMember GetCurrentUser(bool cacheResult)
        {
            CmsMember user = null;
            if (HttpContext.Current != null && HttpContext.Current.User != null)
            {
                user = (CmsMember)SessionHelper.GetValue("CurrentUser");
                if (user == null)
                {
                    if (HTTPHelper.IsUserAuthenticated())
                    {
                        user = CmsMemberHandler.GetByUserName(HTTPHelper.GetUserName());
                    }
                }
            }
            if (user == null)
            {
                // Create public anonymous user
                user = new CmsMember();
                //userCombination.UserObject.IsNetworkUser = false;
            }
            if (cacheResult)
            {
                SetCurrentUser(user);
            }
            return user;
        }

        public void SetCurrentUser(CmsMember uInfo)
        {
            SessionHelper.SetValue("CurrentUser", uInfo);
        }

    }
}